package com.plugin.planilha;

import java.util.HashMap;
import java.util.Map;

import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class AnaliseSetSheet implements ISheet{
	
	private WritableWorkbook workbook;
	private HashMap<String, Map<String, Integer>> map;
	private static WritableSheet toArraySetWS;

	
	public AnaliseSetSheet(WritableWorkbook w, HashMap<String, Map<String, Integer>> map) {
		workbook = w;
		this.map = map;
		toArraySetWS = workbook.createSheet("toArray em Set", 1);
	}

	@Override
	public void criaCabecalho(WritableCellFormat cf)
			throws RowsExceededException, WriteException {
		
		Label label = new Label(0, 0, "Variavel", cf);
		toArraySetWS.addCell(label);
		
		label = new Label(1, 0, "Numero de ToArray", cf);
		toArraySetWS.addCell(label);
		
		label = new Label(2, 0, "toArray", cf);
		toArraySetWS.addCell(label);
		
		label = new Label(4, 0, "Total de toArray()", cf);
		toArraySetWS.addCell(label);
		
		label = new Label(5, 0, "Total variaveis que utilizam toArray()", cf);
		toArraySetWS.addCell(label);
		
		label = new Label(6, 0, "Total variaveis que nao utilizam toArray()", cf);
		toArraySetWS.addCell(label);

		label = new Label(7, 0, "Taxa de variaveis que utilizam toArray()", cf);
		toArraySetWS.addCell(label);
	}

	@Override
	public void addDados() throws RowsExceededException, WriteException {
		
		Label label;
		jxl.write.Number n;
		int indice = 1;
		double total_toarray=0;
		double total_variaveis_no_toarray=0, total_variaveis_toarray=0;
		
		for (String ch : map.keySet()) {
			if (map.get(ch).size() > 0) 
				for (String variavel : map.get(ch).keySet()) {
					int num_toarray = map.get(ch).get(variavel);
					total_toarray += num_toarray;
					boolean toarray = (num_toarray > 0) ? true : false;

					label = new Label(0, indice, variavel);
					toArraySetWS.addCell(label);

					n = new jxl.write.Number(1, indice,
							num_toarray);
					toArraySetWS.addCell(n);
					
					label = new Label(2, indice, Boolean.toString(toarray));
					toArraySetWS.addCell(label);
					
					if (toarray)
						total_variaveis_toarray++;
					else
						total_variaveis_no_toarray++;
					indice++;
				}
		}
		double round_total=0;
		if((total_variaveis_no_toarray + total_variaveis_toarray)!=0){
		double total = (100 * total_variaveis_toarray)
				/ (total_variaveis_no_toarray + total_variaveis_toarray);
		round_total = (double) Math.round(total * 1000) / 1000;
		}
		
		n = new jxl.write.Number(4, 1, (int) total_toarray);
		toArraySetWS.addCell(n);
		
		n = new jxl.write.Number(5, 1, (int) total_variaveis_toarray);
		toArraySetWS.addCell(n);
		
		n = new jxl.write.Number(6, 1, (int) total_variaveis_no_toarray);
		toArraySetWS.addCell(n);
		
		n = new jxl.write.Number(7, 1, round_total);
		toArraySetWS.addCell(n);
	}
	
	
	
}
